package com.cooper.scraper.show;

import java.util.List;

import com.cooper.android.comm.Resource;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.ViewGroup;
import android.widget.AdapterView;

public abstract class FanartsShowView<T extends Resource> {

	protected Context context;
	protected ResourceShowListener<T> showListener;
	private FanartAdapter<T> fanartAdapter;

	public FanartsShowView(Context context) {
		super();
		this.context = context.getApplicationContext();
	}

	public void setAdapter(FanartAdapter<T> fanartAdapter) {
		onNothingResourceSelected();
		this.fanartAdapter = fanartAdapter;
	}

	public FanartAdapter<T> getAdapter() {
		return fanartAdapter;
	}

	public abstract void setCommands(List<ResourceCommandAction<T>> commands);

	// protected abstract FanartImgStrategy getFanartImgStrategy();

	public void setMediasShowListener(ResourceShowListener<T> showListener) {
		this.showListener = showListener;
	}

	public void registerContextMenu(Activity activity) {
		activity.registerForContextMenu(getAdapterView());
	}

	public void registerContextMenu(Fragment fragment) {
		fragment.registerForContextMenu(getAdapterView());
	}

	@SuppressWarnings("unchecked")
	public T getSelectedResource() {
		return (T) getAdapterView().getSelectedItem();
	}

	public void destroy() {
		AdapterView<?> adapterView = getAdapterView();
		if (adapterView != null)
			adapterView.setAdapter(null);
	}

	
	public abstract void removeFrom();
	
	public abstract void showTo(ViewGroup to);

	public abstract void setSelection(int position, boolean animate);

	public abstract int getSelectedPosition();

	public abstract void hideOrShowFanartsView();

	public void resetSelected() {
		T result = getSelectedResource();
		if (result == null) {
			onNothingResourceSelected();
		} else {
			onResourceSelected(result);
		}
	}

	protected abstract void onNothingResourceSelected();

	protected abstract void onResourceSelected(T selected);

	protected abstract AdapterView<?> getAdapterView();

}
